package com.admin.controller;


import cn.hutool.core.io.FileUtil;
import com.admin.entity.Apk;
import com.admin.entity.SystemApk;
import com.admin.service.ISystemApkService;
import com.admin.service.impl.SystemApkServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author marisheng
 * @since 2019-11-21
 */
@RestController
@RequestMapping("/admin/system-apk")
public class SystemApkController {


    @Autowired
    private ISystemApkService systemApkService;

    @Value("${upload.tmp.dir}")
    private String tmp;

    @Value("${upload.dir}")
    private String upload;


    /**
     * 查询系统apk升级包同时搜索应用名称
     * @param page
     * @param limit
     * @param apkName
     * @return
     */
    @PostMapping("/SearchSystemApk")
    public ResponseBean SearchSystemApk(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit,String apkName){
        IPage result=systemApkService.SearchSystemApk(page,limit,apkName);
        return new ResponseBean(0,result.getTotal(),result.getRecords());
    }

    /**
     * 查询所有系统apk并分页
     * @param page
     * @param limit
     * @param systemApkName
     * @return
     */
    @RequestMapping("/searchSystemApkWithPage")
    public ResponseBean searchSystemApkWithPage(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int limit,String systemApkName){
        IPage result=systemApkService.searchSystemApkWithPage(page,limit,systemApkName);
        return new ResponseBean(0,result.getTotal(),result.getRecords());
    }

    /**
     *新增系统apk应用
     * @param systemApk
     * @return
     */
    @RequestMapping(value = "/addSystemApk",method = RequestMethod.POST)
    public ResponseBean addSystemApk(SystemApk systemApk)
    {
        if(systemApkService.selectSystemApkMd5AndApkAdmin(systemApk.getSystemApkMd5())!=null){
            return new ResponseBean(500,null,"文件已上传");
        }
        //得到当前时间
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);
        LocalDateTime uploadTime = LocalDateTime.parse(localTime, df);
        systemApk.setSystemApkUploadTime(uploadTime);
        //截取得到文件名
        String oldPath = systemApk.getSystemApkUrl();
        String newPath = oldPath.substring(oldPath.lastIndexOf("\\")+1);
        int insert = systemApkService.addSystemApk(systemApk);
        if (insert>0)
        {
            //将apk应用文件从临时目录复制到正式目录
            FileUtil.copy(tmp+"/"+newPath,upload+"/"+newPath,true);
            return new ResponseBean(200,"新增成功",systemApk);
        }
        return new ResponseBean(500,"新增失败","");
    }

    /**
     * 根据id修改系统apk
     * @param systemApk
     * @return
     */
    @RequestMapping(value = "/updateSystemApkById",method = RequestMethod.POST)
    public ResponseBean updateSystemApkById(SystemApk systemApk)
    {
        if(systemApkService.selectSystemApkMd5AndApkAdmin(systemApk.getSystemApkMd5())!=null){
            return new ResponseBean(500,null,"文件已上传");
        }

        if(systemApk == null)
        {
            return new ResponseBean(0,null,null);
        }
        //得到当前时间
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time = LocalDateTime.now();
        String localTime = df.format(time);
        LocalDateTime uploadTime = LocalDateTime.parse(localTime, df);
        systemApk.setSystemApkUploadTime(uploadTime);
        //截取得到文件名
        String oldPath = systemApk.getSystemApkUrl();
        String newPath = oldPath.substring(oldPath.lastIndexOf("\\")+1);
        boolean f = systemApkService.updateById(systemApk);
        if(f)
        {
            //将apk应用文件从临时目录复制到正式目录
            FileUtil.copy(tmp+"/"+newPath,upload+"/"+newPath,true);
            return new ResponseBean(200,"编辑成功",systemApk);
        }
        return new ResponseBean(500,"编辑失败",null);
    }

    /**
     * 根据id删除系统apk
     * @param systemApkId
     * @return
     */
    @RequestMapping(value = "deleteSystemAPKById",method = RequestMethod.POST)
    public ResponseBean deleteSystemAPKById (int systemApkId)
    {
        boolean f = systemApkService.removeById(systemApkId);
        int code = f ? 200 : 500;
        return new ResponseBean(code,null,null);
    }

}
